﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;


namespace Snooze.Model
{
    using Castle.ActiveRecord;
    using Castle.ActiveRecord.Framework.Config;

    public sealed class Init
    {
        private static readonly Init instance = new Init();

        private bool initialized = false;

        private Init() { }

        public static Init Instance
        {
            get
            {
                return instance;
            }
        }

        public bool Initialized
        {
            get
            {
                return initialized;
            }
        }

        public void Initialize(string server_path)
        {
            if (initialized) return;

            ActiveRecordStarter.Initialize(System.Reflection.Assembly.GetExecutingAssembly(), GetActiveRecordConfig(server_path));
            initialized = true;
        }

        /// <summary>
        /// The database configuration is HARDCODED(!) in GlobalApplication.cs because the firebird
        /// connection string does not support relative paths to the siteroot
        /// </summary>
        private static InPlaceConfigurationSource GetActiveRecordConfig(string server_path)
        {

            OperatingSystem os = Environment.OSVersion;

            Console.WriteLine("Snooze running on:" + os.Platform);

            string codebase = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase);
            codebase = codebase.Replace("%20", " ");
            string cs = "";

            Hashtable properties = new Hashtable();
            properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
            properties.Add("hibernate.dialect", "NHibernate.Dialect.FirebirdDialect");
            properties.Add("hibernate.use_outer_join", "true");
            properties.Add("show_sql", "true");
            properties.Add("hibernate.query.substitutions", "true=1, false=0, yes=1, no=0");

            if (os.Platform.ToString() == "Unix")
            {
                codebase = codebase.Replace("file:/", "");
                properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.FirebirdDriver");
                cs = "ServerType=1;Database={1}database/snooze.fdb;User=SYSDBA;Password=masterkey;Dialect=3";
            }

            if (os.Platform.ToString() == "Win32NT")
            {
                codebase = codebase.Replace("file:\\", "");
                properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.FirebirdClientDriver");
                cs = "ServerType=1;clientlibrary={0}\\fbembed.dll;Database={1}database\\SNOOZE.FDB;User=SYSDBA;Password=masterkey;Dialect=3";
            }

            cs = string.Format(cs, codebase, server_path);
            properties.Add("hibernate.connection.connection_string", cs);

            InPlaceConfigurationSource source = new InPlaceConfigurationSource();

            source.Add(typeof(ActiveRecordBase), properties);

            return source;

        }
    }

}
